import { Controller, Get, Post, Delete, Put, Query, UseGuards, Body, Req, ForbiddenException, GoneException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Request } from 'express';
import { OrderService } from './order.service';

@UseGuards(AuthGuard('jwt'))
@Controller('order')
export class OrderController {
    constructor(
        private readonly orderService: OrderService
    ) { }
    // 不能添加
    // @Post('add')
    // add(@Body() body: any) {
    //     return this.orderService.add(body);
    // }

    @Delete()
    async delete(@Body() body: any, @Req() req: Request) {
        const order = await this.orderService.getOrderById(body.id);
    
        
        if (!order) {
            throw new GoneException('订单已被删除');
        }
        if (order.buyer_id.id !== req.user['user_id']) {
            console.log(order.buyer_id);
            console.log(req.user['user_id']);
            
            throw new ForbiddenException('没有权限删除当前订单');
        }
        if (order.pay_state === 'SUCCESS') {
            return;
        }
        return this.orderService.delete(body.id);
    }

    @Put('update')
    update(@Body() body: any) {
        return this.orderService.update(body.id, body);
    }

    @Get()
    async index(@Query() query: any, @Req() req: Request) {
        if (req.user && req.user['identity'] === 'admin') {
            return await this.orderService.findAll(query);
        }
    }

    @Get('getOrdersByUserId')
    getOrdersByUserId(@Query() query: any, @Req() req: Request) {
        if (req.user && (req.user['identity'] === 'admin' || query.userId == req.user['user_id'])) {
            return this.orderService.getOrdersByUserId(query);
        }
    }

    @Get('getOrderByProduct')
    getOrderByProduct(@Query() query: any, @Req() req: Request) {
        let buyer;
        if (req.user['identity'] === 'other') {
            buyer = {
                id: req.user['user_id']
            }
        }
        return this.orderService.getOrderByProduct({ id: query.productId }, buyer);
    }

    @Get('getOrdersByState')
    getOrdersByState(@Query() query: any, @Req() req: Request) {
        return this.orderService.getOrdersByState(query, req.user['user_id'], req);
    }

    @Get('getOrdersByStateSkip')//分页
    getOrdersByStateSkip(@Query() query: any, @Req() req: Request) {
        return this.orderService.getOrdersByStateSkip(query, req.user['user_id'], req);
    }
    

    @Get('number')
    async getOrderByNumber(@Query() query: any, @Req() req: Request) {
        const order = await this.orderService.getOrderByNumber(query.order_number);
        if (order.pay_state === 'NOTPAY' || !req.headers['user-agent'].includes('MicroMessenger')) { // 未支付订单  或者不在移动端移除视频和试卷地址
            const productInfo = order.product.product_info;
            for (let i = 0; i < productInfo?.length; i++) {
                const info = productInfo[i];
                // info.product_url = null;
                // info.video && (info.video.file_path = null);
            }
        }
        if (order && req.user['identity'] !== 'admin' && order.buyer_id.id !== req.user['user_id']) {
            throw new ForbiddenException('无权查看此订单');
        }
        return order;
    }
    @Get('getOrderById')
    getOrdersById(@Query() query: any, @Req() req: Request) {
        return this.orderService.getOrderById(query.id);
    }

}
